package cn.netjava.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.netjava.dbutil.Conn2DB;
import cn.netjava.pojo.Photo;

/**
 * this class is use for dealing with user's photo
 * @author Administrator
 *
 */
public class PhotoDao {
    public static PhotoDao pdao;
	
	/*make PhotoDao pravate*/
	private PhotoDao(){}
	
	/*obtain a PhotoDao object*/
	public static PhotoDao getPhotoDao(){
		if(null==pdao){
			pdao = new PhotoDao();
		}
		return pdao;
	}
	
	/**
	 * to add a photo to the corresponding photo album
	 * @param photoPath
	 * @param photoDesc
	 * @param albumID
	 * @return
	 */
	public boolean addPhoto(String photoPath,String photoDesc,int albumID){
		String sql ="insert into tbl_photos(photopath,photodesc,ins_albumid,ins_dt)values(?,?,?,?)";
		try{
			java.sql.Connection conn =Conn2DB.getConn();
			java.sql.PreparedStatement pstmt =conn.prepareStatement(sql);
			
			pstmt.setString(1, photoPath);
			pstmt.setString(2, photoDesc);
			pstmt.setInt(3, albumID);
			/*obtain the system time*/
			java.sql.Timestamp timeStamp =new java.sql.Timestamp(System.currentTimeMillis());
			String ins_td =timeStamp.toString().substring(0,timeStamp.toString().lastIndexOf("."));
			pstmt.setString(4, ins_td);   

            pstmt.executeUpdate();			
			pstmt.close();						
			return true;
		}catch(Exception ef){
			ef.printStackTrace();
		}
		return false;
	}
	
	/**
	 * to get one photo album's all photos according to album ID
	 * @param albumID
	 * @return
	 */
	public List<Photo> getAllPhotoFromAlbumID(int albumID){
		List<Photo> photoList = new ArrayList<Photo>();
		String sql ="select * from tbl_photos where ins_albumid=?";
		try{
			java.sql.Connection conn =Conn2DB.getConn();
			java.sql.PreparedStatement pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, albumID);
			
			ResultSet rs =pstmt.executeQuery();
			while(rs.next()){
				Photo photo =new Photo();
				
				photo.setId(rs.getInt("id"));
				photo.setPhotoPath(rs.getString("photopath"));
				photo.setPhotoDesc(rs.getString("photodesc"));
				photo.setPhotoAlbum(PhotoAlbumDao.getPhotoAlbumDao().getPhotoAlbumFromAlbumID(rs.getInt("ins_albumid")));
			    photo.setIns_td(rs.getString("ins_dt"));
			    
			    photoList.add(photo);
			}
			pstmt.close();					
			rs.close();
		}catch(Exception ef){
			ef.printStackTrace();
		}	
		return photoList;
	}
	
	/**
	 * to get a photo according to photo ID
	 * @param photoID
	 * @return
	 */
	public Photo getPhotoFromPhotoID(int photoID){
		Photo photo =new Photo();
		String sql ="select * from tbl_photos where id=?";
		try{
			java.sql.Connection conn =Conn2DB.getConn();
			java.sql.PreparedStatement pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, photoID);
			
			ResultSet rs =pstmt.executeQuery();
			if(rs.next()){				
				photo.setId(rs.getInt("id"));
				photo.setPhotoPath(rs.getString("photopath"));
				photo.setPhotoDesc(rs.getString("photodesc"));
				photo.setPhotoAlbum(PhotoAlbumDao.getPhotoAlbumDao().getPhotoAlbumFromAlbumID(rs.getInt("ins_albumid")));
			    photo.setIns_td(rs.getString("ins_dt"));
			}
			pstmt.close();					
			rs.close();
		}catch(Exception ef){
			ef.printStackTrace();
		}	
		return photo;
	}
	
	/**
	 * to get 4 lately photo from mysql
	 * @return
	 */
	public List<Photo> get4LatelyPhoto(){
		List<Photo> photoList =new ArrayList<Photo>();
		String sql ="select * from tbl_photos order by ins_dt desc limit 0,4";
		try{
			java.sql.Connection conn =Conn2DB.getConn();
			java.sql.PreparedStatement pstmt =conn.prepareStatement(sql);
			
			ResultSet rs =pstmt.executeQuery();
			while(rs.next()){
				Photo photo =new Photo();
				
				photo.setId(rs.getInt("id"));
				photo.setPhotoPath(rs.getString("photopath"));
				photo.setPhotoDesc(rs.getString("photodesc"));
				photo.setPhotoAlbum(PhotoAlbumDao.getPhotoAlbumDao().getPhotoAlbumFromAlbumID(rs.getInt("ins_albumid")));
			    photo.setIns_td(rs.getString("ins_dt"));
			    
			    photoList.add(photo);
			}
			pstmt.close();					
			rs.close();
		}catch(Exception ef){
			ef.printStackTrace();
		}	
		return photoList;
	}

}
